﻿// Decompiled with JetBrains decompiler
// Type: pvdcd.Part
// Assembly: pvdcd, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8BE7F426-72A0-448B-ADB3-CA2AC8E1D830
// Assembly location: C:\Users\KasimGulcan\Desktop\Kosgeb\DynaCAD\Project\DynaCAD\bin\Debug\pvdcd.dll

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using Template;
using XamlEditor;
using XO1;


namespace pvdcd
{
    [Serializable]
    public class Part
    {
        public Template.Type PartType;
        public string PartName;
        public string TemplateName;
        public string PreparedBy;
        public List<PartPoint> FrontLeftPoints;
        public List<PartPoint> FrontRightPoints;
        public List<PartPoint> BackLeftPoints;
        public List<PartPoint> BackRightPoints;
        public List<DetailView> DetailViews;
        [NonSerialized]
        public Canvas FrontLeftView;
        [NonSerialized]
        public Canvas FrontRightView;
        [NonSerialized]
        public Canvas BackLeftView;
        [NonSerialized]
        public Canvas BackRightView;

        public Part()
        {
            this.FrontLeftPoints = new List<PartPoint>();
            this.FrontRightPoints = new List<PartPoint>();
            this.BackLeftPoints = new List<PartPoint>();
            this.BackRightPoints = new List<PartPoint>();
            this.FrontLeftView = new Canvas();
            this.FrontRightView = new Canvas();
            this.BackLeftView = new Canvas();
            this.BackRightView = new Canvas();
            this.DetailViews = new List<DetailView>();
        }

        public void PartUpDate()
        {
            foreach (PartPoint partPoint in this.FrontLeftPoints)
                partPoint.UpdatePartPoint();
            foreach (PartPoint partPoint in this.FrontRightPoints)
                partPoint.UpdatePartPoint();
            foreach (PartPoint partPoint in this.BackLeftPoints)
                partPoint.UpdatePartPoint();
            foreach (PartPoint partPoint in this.BackRightPoints)
                partPoint.UpdatePartPoint();
            foreach (DetailView detailView in this.DetailViews)
                detailView.UpdateDetailView();
        }

        public void DrivePart()
        {
            this.PartType.DriveType();
            foreach (PartPoint partPoint in this.FrontLeftPoints)
                partPoint.DrivePoint();
            foreach (PartPoint partPoint in this.FrontRightPoints)
                partPoint.DrivePoint();
            foreach (PartPoint partPoint in this.BackLeftPoints)
                partPoint.DrivePoint();
            foreach (PartPoint partPoint in this.BackRightPoints)
                partPoint.DrivePoint();
            foreach (DetailView detailView in this.DetailViews)
                detailView.DriveDetailView();
        }

        public Canvas GetFrontLeftView()
        {
            Canvas canvas = (Canvas)CloneUIElement.cloneElement((UIElement)this.FrontLeftView);
            TransformGroup transformGroup = new TransformGroup();
            ScaleTransform scaleTransform = new ScaleTransform();
            TranslateTransform translateTransform = new TranslateTransform();
            RotateTransform rotateTransform = new RotateTransform();
            int Number1 = this.FrontLeftPoints[0].PointNumber;
            int Number2 = this.FrontLeftPoints[1].PointNumber;
            PartPoint RP1 = new PartPoint(0.0, 0.0, 1);
            PartPoint RP2 = new PartPoint(0.0, 0.0, 1);
            this.PartType.FrontLeftPoints.ForEach((Action<PartPoint>)(p =>
            {
                if (p.PointNumber == Number1)
                {
                    RP1 = p;
                }
                else
                {
                    if (p.PointNumber != Number2)
                        return;
                    RP2 = p;
                }
            }));
            translateTransform.X = RP1.X - this.FrontLeftPoints[0].X;
            translateTransform.Y = RP1.Y - this.FrontLeftPoints[0].Y;
            rotateTransform.CenterX = this.FrontLeftPoints[0].X;
            rotateTransform.CenterY = this.FrontLeftPoints[0].Y;
            rotateTransform.Angle = Vector.AngleBetween(new Vector(this.FrontLeftPoints[1].X - this.FrontLeftPoints[0].X, this.FrontLeftPoints[1].Y - this.FrontLeftPoints[0].Y), new Vector(RP2.X - RP1.X, RP2.Y - RP1.Y));
            scaleTransform.ScaleX = Math.Sqrt(Math.Pow(RP1.X - RP2.X, 2.0) + Math.Pow(RP1.Y - RP2.Y, 2.0)) / Math.Sqrt(Math.Pow(this.FrontLeftPoints[0].X - this.FrontLeftPoints[1].X, 2.0) + Math.Pow(this.FrontLeftPoints[0].Y - this.FrontLeftPoints[1].Y, 2.0));
            scaleTransform.ScaleY = scaleTransform.ScaleX;
            scaleTransform.CenterX = this.FrontLeftPoints[0].X;
            scaleTransform.CenterY = this.FrontLeftPoints[0].Y;
            transformGroup.Children.Add((Transform)scaleTransform);
            transformGroup.Children.Add((Transform)rotateTransform);
            transformGroup.Children.Add((Transform)translateTransform);
            canvas.RenderTransform = (Transform)transformGroup;
            return canvas;
        }

        public Canvas GetFrontRightView()
        {
            Canvas canvas = (Canvas)CloneUIElement.cloneElement((UIElement)this.FrontRightView);
            TransformGroup transformGroup = new TransformGroup();
            ScaleTransform scaleTransform = new ScaleTransform();
            TranslateTransform translateTransform = new TranslateTransform();
            RotateTransform rotateTransform = new RotateTransform();
            int Number1 = this.FrontRightPoints[0].PointNumber;
            int Number2 = this.FrontRightPoints[1].PointNumber;
            PartPoint RP1 = new PartPoint(0.0, 0.0, 1);
            PartPoint RP2 = new PartPoint(0.0, 0.0, 1);
            this.PartType.FrontRightPoints.ForEach((Action<PartPoint>)(p =>
            {
                if (p.PointNumber == Number1)
                {
                    RP1 = p;
                }
                else
                {
                    if (p.PointNumber != Number2)
                        return;
                    RP2 = p;
                }
            }));
            translateTransform.X = RP1.X - this.FrontRightPoints[0].X;
            translateTransform.Y = RP1.Y - this.FrontRightPoints[0].Y;
            rotateTransform.CenterX = this.FrontRightPoints[0].X;
            rotateTransform.CenterY = this.FrontRightPoints[0].Y;
            rotateTransform.Angle = Vector.AngleBetween(new Vector(this.FrontRightPoints[1].X - this.FrontRightPoints[0].X, this.FrontRightPoints[1].Y - this.FrontRightPoints[0].Y), new Vector(RP2.X - RP1.X, RP2.Y - RP1.Y));
            scaleTransform.ScaleX = Math.Sqrt(Math.Pow(RP1.X - RP2.X, 2.0) + Math.Pow(RP1.Y - RP2.Y, 2.0)) / Math.Sqrt(Math.Pow(this.FrontRightPoints[0].X - this.FrontRightPoints[1].X, 2.0) + Math.Pow(this.FrontRightPoints[0].Y - this.FrontRightPoints[1].Y, 2.0));
            scaleTransform.ScaleY = scaleTransform.ScaleX;
            scaleTransform.CenterX = this.FrontRightPoints[0].X;
            scaleTransform.CenterY = this.FrontRightPoints[0].Y;
            transformGroup.Children.Add((Transform)scaleTransform);
            transformGroup.Children.Add((Transform)rotateTransform);
            transformGroup.Children.Add((Transform)translateTransform);
            canvas.RenderTransform = (Transform)transformGroup;
            return canvas;
        }

        public Canvas GetBackLeftView()
        {
            Canvas canvas = (Canvas)CloneUIElement.cloneElement((UIElement)this.BackLeftView);
            TransformGroup transformGroup = new TransformGroup();
            ScaleTransform scaleTransform = new ScaleTransform();
            TranslateTransform translateTransform = new TranslateTransform();
            RotateTransform rotateTransform = new RotateTransform();
            int Number1 = this.BackLeftPoints[0].PointNumber;
            int Number2 = this.BackLeftPoints[1].PointNumber;
            PartPoint RP1 = new PartPoint(0.0, 0.0, 1);
            PartPoint RP2 = new PartPoint(0.0, 0.0, 1);
            this.PartType.BackLeftPoints.ForEach((Action<PartPoint>)(p =>
            {
                if (p.PointNumber == Number1)
                {
                    RP1 = p;
                }
                else
                {
                    if (p.PointNumber != Number2)
                        return;
                    RP2 = p;
                }
            }));
            translateTransform.X = RP1.X - this.BackLeftPoints[0].X;
            translateTransform.Y = RP1.Y - this.BackLeftPoints[0].Y;
            rotateTransform.CenterX = this.BackLeftPoints[0].X;
            rotateTransform.CenterY = this.BackLeftPoints[0].Y;
            rotateTransform.Angle = Vector.AngleBetween(new Vector(this.BackLeftPoints[1].X - this.BackLeftPoints[0].X, this.BackLeftPoints[1].Y - this.BackLeftPoints[0].Y), new Vector(RP2.X - RP1.X, RP2.Y - RP1.Y));
            scaleTransform.ScaleX = Math.Sqrt(Math.Pow(RP1.X - RP2.X, 2.0) + Math.Pow(RP1.Y - RP2.Y, 2.0)) / Math.Sqrt(Math.Pow(this.BackLeftPoints[0].X - this.BackLeftPoints[1].X, 2.0) + Math.Pow(this.BackLeftPoints[0].Y - this.BackLeftPoints[1].Y, 2.0));
            scaleTransform.ScaleY = scaleTransform.ScaleX;
            scaleTransform.CenterX = this.BackLeftPoints[0].X;
            scaleTransform.CenterY = this.BackLeftPoints[0].Y;
            transformGroup.Children.Add((Transform)scaleTransform);
            transformGroup.Children.Add((Transform)rotateTransform);
            transformGroup.Children.Add((Transform)translateTransform);
            canvas.RenderTransform = (Transform)transformGroup;
            return canvas;
        }

        public Canvas GetBackRightView()
        {
            Canvas canvas = (Canvas)CloneUIElement.cloneElement((UIElement)this.BackRightView);
            TransformGroup transformGroup = new TransformGroup();
            ScaleTransform scaleTransform = new ScaleTransform();
            TranslateTransform translateTransform = new TranslateTransform();
            RotateTransform rotateTransform = new RotateTransform();
            int Number1 = this.BackRightPoints[0].PointNumber;
            int Number2 = this.BackRightPoints[1].PointNumber;
            PartPoint RP1 = new PartPoint(0.0, 0.0, 1);
            PartPoint RP2 = new PartPoint(0.0, 0.0, 1);
            this.PartType.BackRightPoints.ForEach((Action<PartPoint>)(p =>
            {
                if (p.PointNumber == Number1)
                {
                    RP1 = p;
                }
                else
                {
                    if (p.PointNumber != Number2)
                        return;
                    RP2 = p;
                }
            }));
            translateTransform.X = RP1.X - this.BackRightPoints[0].X;
            translateTransform.Y = RP1.Y - this.BackRightPoints[0].Y;
            rotateTransform.CenterX = this.BackRightPoints[0].X;
            rotateTransform.CenterY = this.BackRightPoints[0].Y;
            rotateTransform.Angle = Vector.AngleBetween(new Vector(this.BackRightPoints[1].X - this.BackRightPoints[0].X, this.BackRightPoints[1].Y - this.BackRightPoints[0].Y), new Vector(RP2.X - RP1.X, RP2.Y - RP1.Y));
            scaleTransform.ScaleX = Math.Sqrt(Math.Pow(RP1.X - RP2.X, 2.0) + Math.Pow(RP1.Y - RP2.Y, 2.0)) / Math.Sqrt(Math.Pow(this.BackRightPoints[0].X - this.BackRightPoints[1].X, 2.0) + Math.Pow(this.BackRightPoints[0].Y - this.BackRightPoints[1].Y, 2.0));
            scaleTransform.ScaleY = scaleTransform.ScaleX;
            scaleTransform.CenterX = this.BackRightPoints[0].X;
            scaleTransform.CenterY = this.BackRightPoints[0].Y;
            transformGroup.Children.Add((Transform)scaleTransform);
            transformGroup.Children.Add((Transform)rotateTransform);
            transformGroup.Children.Add((Transform)translateTransform);
            canvas.RenderTransform = (Transform)transformGroup;
            return canvas;
        }

        public void aSave()
        {
            string path = Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + ".obj";
            if (!Directory.Exists(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\"))
                Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\");

            FileStream fileStream1 = new FileStream(path, FileMode.Create);
            new BinaryFormatter().Serialize((Stream)fileStream1, (object)this);
            fileStream1.Close();
            File.WriteAllText(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_OnSol.xaml", string.Empty);
            FileStream fileStream2 = new FileStream(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_OnSol.xaml", FileMode.OpenOrCreate);
            XamlWriter.Save((object)this.FrontLeftView, (Stream)fileStream2);
            fileStream2.Close();
            int num = 0;
            foreach (DetailView detailView in this.DetailViews)
            {
                File.WriteAllText(Directory.GetCurrentDirectory() + (object)"\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_DetailView" + (string)(object)Convert.ToSingle(num) + ".xaml", string.Empty);
                FileStream fileStream3 = new FileStream(Directory.GetCurrentDirectory() + (object)"\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_DetailView" + (string)(object)Convert.ToSingle(num) + ".xaml", FileMode.OpenOrCreate);
                XamlWriter.Save((object)detailView.Detail, (Stream)fileStream3);
                fileStream3.Close();
                ++num;
            }



            if (this.PartType.FrontBack && this.PartType.LeftRight)
            {
                File.WriteAllText(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_OnSağ.xaml", string.Empty);
                FileStream fileStream3 = new FileStream(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_OnSağ.xaml", FileMode.OpenOrCreate);
                XamlWriter.Save((object)this.FrontRightView, (Stream)fileStream3);
                fileStream3.Close();
                File.WriteAllText(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_ArkaSol.xaml", string.Empty);
                FileStream fileStream4 = new FileStream(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_ArkaSol.xaml", FileMode.OpenOrCreate);
                XamlWriter.Save((object)this.BackLeftView, (Stream)fileStream4);
                fileStream4.Close();
                File.WriteAllText(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_ArkaSağ.xaml", string.Empty);
                FileStream fileStream5 = new FileStream(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_ArkaSağ.xaml", FileMode.OpenOrCreate);
                XamlWriter.Save((object)this.BackRightView, (Stream)fileStream5);
                fileStream5.Close();
            }


            else if (this.PartType.FrontBack && !this.PartType.LeftRight)
            {
                File.WriteAllText(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_ArkaSol.xaml", string.Empty);
                FileStream fileStream3 = new FileStream(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_ArkaSol.xaml", FileMode.OpenOrCreate);
                XamlWriter.Save((object)this.BackLeftView, (Stream)fileStream3);
                fileStream3.Close();
            }


            else
            {
                if (this.PartType.FrontBack || !this.PartType.LeftRight)
                    return;
                File.WriteAllText(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_OnSağ.xaml", string.Empty);
                FileStream fileStream3 = new FileStream(Directory.GetCurrentDirectory() + "\\Parts\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_OnSağ.xaml", FileMode.OpenOrCreate);
                XamlWriter.Save((object)this.FrontRightView, (Stream)fileStream3);
                fileStream3.Close();
            }



        }

        public void aSave(string DirectoryName)
        {
            string path = DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + ".obj";
            if (!Directory.Exists(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\"))
                Directory.CreateDirectory(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\");



            FileStream fileStream1 = new FileStream(path, FileMode.Create);
            new BinaryFormatter().Serialize((Stream)fileStream1, (object)this);
            fileStream1.Close();
            File.WriteAllText(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_OnSol.xaml", string.Empty);
            FileStream fileStream2 = new FileStream(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_OnSol.xaml", FileMode.OpenOrCreate);
            XamlWriter.Save((object)this.FrontLeftView, (Stream)fileStream2);
            fileStream2.Close();
            int num = 0;
            foreach (DetailView detailView in this.DetailViews)
            {
                File.WriteAllText(DirectoryName + (object)"\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_DetailView" + (string)(object)Convert.ToSingle(num) + ".xaml", string.Empty);
                FileStream fileStream3 = new FileStream(DirectoryName + (object)"\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_DetailView" + (string)(object)Convert.ToSingle(num) + ".xaml", FileMode.OpenOrCreate);
                XamlWriter.Save((object)detailView.Detail, (Stream)fileStream3);
                fileStream3.Close();
                ++num;
            }
            if (this.PartType.FrontBack && this.PartType.LeftRight)
            {
                File.WriteAllText(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_OnSağ.xaml", string.Empty);
                FileStream fileStream3 = new FileStream(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_OnSağ.xaml", FileMode.OpenOrCreate);
                XamlWriter.Save((object)this.FrontRightView, (Stream)fileStream3);
                fileStream3.Close();
                File.WriteAllText(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_ArkaSol.xaml", string.Empty);
                FileStream fileStream4 = new FileStream(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_ArkaSol.xaml", FileMode.OpenOrCreate);
                XamlWriter.Save((object)this.BackLeftView, (Stream)fileStream4);
                fileStream4.Close();
                File.WriteAllText(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_ArkaSağ.xaml", string.Empty);
                FileStream fileStream5 = new FileStream(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_ArkaSağ.xaml", FileMode.OpenOrCreate);
                XamlWriter.Save((object)this.BackRightView, (Stream)fileStream5);
                fileStream5.Close();
            }
            else if (this.PartType.FrontBack && !this.PartType.LeftRight)
            {
                File.WriteAllText(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_ArkaSol.xaml", string.Empty);
                FileStream fileStream3 = new FileStream(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_ArkaSol.xaml", FileMode.OpenOrCreate);
                XamlWriter.Save((object)this.BackLeftView, (Stream)fileStream3);
                fileStream3.Close();
            }
            else
            {
                if (this.PartType.FrontBack || !this.PartType.LeftRight)
                    return;
                File.WriteAllText(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_OnSağ.xaml", string.Empty);
                FileStream fileStream3 = new FileStream(DirectoryName + "\\" + this.TemplateName + "\\" + this.PartType.PartType + "\\" + this.PartName + "_OnSağ.xaml", FileMode.OpenOrCreate);
                XamlWriter.Save((object)this.FrontRightView, (Stream)fileStream3);
                fileStream3.Close();
            }
        }


   
        public static Part aLoadPart(string FileName)
        {
            Part part1 = new Part();
            FileStream fileStream = new FileStream(FileName, FileMode.Open);
            Part part2 = (Part)new BinaryFormatter().Deserialize((Stream)fileStream);
            part2.DrivePart();
            fileStream.Close();
            Canvas canvas1 = XamlFileOperations.OpenXaml(FileName.Replace(".obj", "_OnSol.xaml"));
            part2.FrontLeftView = canvas1;
            int num = 0;
            foreach (DetailView detailView in part2.DetailViews)
            {
                detailView.Detail = XamlFileOperations.OpenXaml(FileName.Replace(".obj", "_DetailView" + Convert.ToString(num) + ".xaml"));
                ++num;
            }
            if (part2.PartType.FrontBack && part2.PartType.LeftRight)
            {
                Canvas canvas2 = XamlFileOperations.OpenXaml(FileName.Replace(".obj", "_OnSağ.xaml"));
                Canvas canvas3 = XamlFileOperations.OpenXaml(FileName.Replace(".obj", "_ArkaSol.xaml"));
                Canvas canvas4 = XamlFileOperations.OpenXaml(FileName.Replace(".obj", "_ArkaSağ.xaml"));
                part2.FrontRightView = canvas2;
                part2.BackLeftView = canvas3;
                part2.BackRightView = canvas4;
            }
            else if (part2.PartType.FrontBack && !part2.PartType.LeftRight)
            {
                Canvas canvas2 = XamlFileOperations.OpenXaml(FileName.Replace(".obj", "_ArkaSol.xaml"));
                part2.BackLeftView = canvas2;
            }
            else if (!part2.PartType.FrontBack && part2.PartType.LeftRight)
            {
                Canvas canvas2 = XamlFileOperations.OpenXaml(FileName.Replace(".obj", "_OnSağ.xaml"));
                part2.FrontRightView = canvas2;
            }
            return part2;
        }
    }
}